﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc @Copy Right 2019
// 文件： GovLogOperateRepository.cs
// 项目名称：斑马车务
// 创建时间：2019-01-09
// 负责人：lsc
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using CGT.Infrastructure.Entity;
using CGT.Infrastructure.Dto;
using Dapper.Extensions;
using System.Linq;
using Dapper;

namespace CGT.Infrastructure.Repository
{
	/// <summary>
	/// 仓储层
	/// </summary>	
	public class GovLogOperateRepository:BaseRepository<GovLogOperate>
	{		
    
        #region 构造

        public GovLogOperateRepository() : base(){}

        public GovLogOperateRepository(string connectionString) : base(connectionString){}

        public GovLogOperateRepository(DatabaseContext dbContext) : base(dbContext){}

        #endregion

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="incidentType"></param>
        /// <param name="beginDate"></param>
        /// <param name="endDate"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public Page<GovLogOperateDto> GetList(ScLogOperate condition)
        {
            string sql = @"SELECT o.*,s.MenuName+'-'+l.MenuName as ModuleName,u.realname as UserName
			  FROM [GovLogOperate] o WITH(NOLOCK)
			  LEFT JOIN GovMenu l WITH(NOLOCK) ON l.MenuCode=o.ModuleId
			  LEFT JOIN GovMenu s WITH(NOLOCK) ON s.MenuId=l.ParentID
			  LEFT JOIN GovUser u WITH(NOLOCK) ON u.userid=o.userid WHERE 1=1";
            StringBuilder sb = new StringBuilder(sql);
            if (condition.MenuId > 0)
            {
                sb.Append(" and s.MenuId=@MenuId");
            }
            if (condition.MenuSubId > 0)
            {
                sb.Append(" and l.MenuId=@MenuSubId");
            }
            if (condition.LogId > 0)
            {
                sb.Append(" AND LogId = @LogId");
            }
            if (!string.IsNullOrWhiteSpace(condition.StartDate))
            {
                sb.Append(" AND CreatedDate > @BeginDate");
            }
            if (!string.IsNullOrWhiteSpace(condition.EndDate))
            {
                sb.Append(" AND CreatedDate <= DATEADD(dd,1,@EndDate)");
            }
            sb.Append(" Order by LogId DESC");
            return dbContext.Connection.Page<GovLogOperateDto>(condition.PageIndex, condition.PageSize, sb.ToString(), new { MenuId = condition.MenuId, MenuSubId = condition.MenuSubId, LogId = condition.LogId, BeginDate = condition.StartDate, EndDate = condition.EndDate });
        }

        /// <summary>
        /// 查询统计信息
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public List<LogOperateStatisticDto> SearchStatisticalInformation(ScLogOperate condition)
        {
            List<LogOperateStatisticDto> list = new List<LogOperateStatisticDto>();
            StringBuilder sb = new StringBuilder();
            sb.Append("select b.MenuName as ModuleName,count(1) as OperateCount from GovLogOperate a with(nolock) inner join GovMenu b with(nolock) on a.moduleid = b.MenuCode where 1=1");
            if (condition.MenuId > 0)
            {
                sb.Append(" and b.ParentID=@MenuId");
            }
            if (condition.MenuSubId > 0)
            {
                sb.Append(" and b.ModuleId=@MenuSubId");
            }
            if (condition.StartDate != "")
            {
                sb.Append(" and a.CreatedDate>=@StartDate");
            }
            if (condition.EndDate != "")
            {
                sb.Append(" and a.CreatedDate<DATEADD(dd,1,@EndDate)");
            }
            sb.Append(" group by b.MenuName");
            return dbContext.Connection.Query<LogOperateStatisticDto>(sb.ToString(), new { StartDate = condition.StartDate, EndDate = condition.EndDate, MenuId = condition.MenuId, MenuSubId= condition.MenuSubId }).ToList();
        }
    }
}